package com.gzy.wb.controller;

import com.gzy.wb.controller.para.MyLogPara;
import com.gzy.wb.core.Page;
import com.gzy.wb.core.R;
import com.gzy.wb.model.MyLog;
import com.gzy.wb.model.User;
import com.gzy.wb.server.MyLogServer;
import com.gzy.wb.server.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/mylog")
public class MyLogController {

    @Autowired
    MyLogServer server;
    @Autowired
    UserServer userServer;

    @GetMapping("/")
    R<List<MyLog>> index(@AuthenticationPrincipal UserDetails user){
        User userByName = userServer.findUserByName(user.getUsername());
        return new R<>(){{
            setData(server.findAll(userByName.getId()));
        }};
    }


    @GetMapping("/page")
    R<Page<MyLog>> page(@AuthenticationPrincipal UserDetails user,Long page,Long size){

        User userByName = userServer.findUserByName(user.getUsername());
        Page<MyLog> userByPage = server.findUserByPage(userByName.getId(), page, size);

        return new R<>(){{
            setData(userByPage);
        }};
    }

    @GetMapping("/{id}")
    R<MyLog> show(@PathVariable Long id , @AuthenticationPrincipal UserDetails user){
        User userByName = userServer.findUserByName(user.getUsername());
        MyLog v = server.findMyLogByIdAndUserid(id,userByName.getId());
        return new R<>(){{
            setData(v);
        }};
    }

    @PostMapping("/")
    R<MyLog> save(@AuthenticationPrincipal UserDetails user,@Valid @RequestBody MyLogPara log){
        User userByName = userServer.findUserByName(user.getUsername());
        MyLog myLog = server.create(userByName.getId(), log);
        return new R<>(){{
            setData(myLog);
        }};
    }

    @DeleteMapping("/{id}")
    R delete(@AuthenticationPrincipal UserDetails user,@PathVariable Long id){
        User userByName = userServer.findUserByName(user.getUsername());
        server.delete(userByName.getId(),id);
        return new R();
    }
    @PutMapping("/{id}")
    R<MyLog> update(@AuthenticationPrincipal UserDetails user,@PathVariable Long id,@Valid @RequestBody MyLogPara log){
        User userByName = userServer.findUserByName(user.getUsername());
        MyLog myLog = server.update(userByName.getId(),id,log);
        return new R<MyLog>(){{
            setData(myLog);
        }};
    }

    // Rest api 规范，
    //  一种资源
    // 增  save add             POST   /
    // 删  del remove destroy   DELETE  /{id}
    // 改  update                PUT     /{id}    {"name":"lxf2"}
    // 查                        GET
    //  获取列表  index  getList          /
    // 分页                               /   +  查询参数   query parameter   https://www.baidu.com/s?page=10&size=10
    // 单查  show                         /{id}
}


